package com.example.administrator.managesystem.activity.my_profile.typein;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.managesystem.R;
import com.example.administrator.managesystem.manager.RxManager;
import com.example.administrator.managesystem.utils.ConstantUtil;
import com.example.administrator.managesystem.utils.LogUtil;
import com.example.administrator.managesystem.utils.StatusBarUtil;
import com.example.administrator.managesystem.utils.ToastUtils;
import com.example.administrator.managesystem.view.NoDoubleClickListener;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.photopicker.activity.BGAPhotoPickerActivity;
import cn.bingoogolapple.qrcode.core.BarcodeType;
import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zbar.BarcodeFormat;
import cn.bingoogolapple.qrcode.zbar.ZBarView;

public class CodeScanActivity extends AppCompatActivity implements QRCodeView.Delegate {
    private static final String TAG = CodeScanActivity.class.getSimpleName();
    private static final int REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY = 666;
    private boolean isOpen;
    private ZBarView mZBarView;
    private TextView flash;
    private RxManager mRxManager;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_scan);
        //状态栏
        StatusBarUtil.setWindowStatusBarColor(this,R.color.black);
        //竖屏显示
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mRxManager=new RxManager();
        mZBarView = findViewById(R.id.zbarview);
        flash=findViewById(R.id.open_flashlight);
        mZBarView.setDelegate(this);
        flash.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if(isOpen){
                    mZBarView.closeFlashlight(); // 关闭闪光灯
                    flash.setText("打开闪光灯");
                    isOpen=false;
                }else {
                    mZBarView.openFlashlight(); // 打开闪光灯
                    flash.setText("关闭闪光灯");
                    isOpen=true;
                }

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        mZBarView.startCamera(); // 打开后置摄像头开始预览，但是并未开始识别
//        mZBarView.startCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); // 打开前置摄像头开始预览，但是并未开始识别

        mZBarView.startSpotAndShowRect(); // 显示扫描框，并开始识别
    }

    @Override
    protected void onStop() {
        mZBarView.stopCamera(); // 关闭摄像头预览，并且隐藏扫描框
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        mZBarView.onDestroy(); // 销毁二维码扫描控件
        if (mRxManager != null)
            mRxManager.clear();
        super.onDestroy();
    }

    private void vibrate() {
        Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
        vibrator.vibrate(200);
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        Log.i(TAG, "result:" + result);
        LogUtil.e("onScanQRCodeSuccess --- code" + result);
        getRxManager().post(ConstantUtil.UPDATE_CODE,result);
        ToastUtils.showToast(CodeScanActivity.this,result);
        vibrate();
        mZBarView.startSpot(); // 开始识别
        finish();
    }

    @Override
    public void onCameraAmbientBrightnessChanged(boolean isDark) {
        // 这里是通过修改提示文案来展示环境是否过暗的状态，接入方也可以根据 isDark 的值来实现其他交互效果
        String tipText = mZBarView.getScanBoxView().getTipText();
        String ambientBrightnessTip = "\n环境过暗，请打开闪光灯";
        if (isDark) {
            if (!tipText.contains(ambientBrightnessTip)) {
                mZBarView.getScanBoxView().setTipText(tipText + ambientBrightnessTip);

            }
        } else {

            if (tipText.contains(ambientBrightnessTip)) {
                tipText = tipText.substring(0, tipText.indexOf(ambientBrightnessTip));
                mZBarView.getScanBoxView().setTipText(tipText);

            }
        }
    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        Log.e(TAG, "打开相机出错");
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        mZBarView.showScanRect();

        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_CHOOSE_QRCODE_FROM_GALLERY) {
            final String picturePath = BGAPhotoPickerActivity.getSelectedPhotos(data).get(0);
            mZBarView.decodeQRCode(picturePath);
        }
    }
    public RxManager getRxManager() {
        return mRxManager;
    }

}